<?php

namespace app\common\model\Coupon;
// 引入模型
use think\Model;

// 订单产品的模型
class Coupon extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'coupon';

    // 自动插入的时间戳字段
    protected $autoWriteTimestamp = true;

    //  创建时间字段
    protected $createTime = 'createtime';
    protected $updateTime = false;

    protected $append  = [
        'createtime_text', 
        'endtime_text',
        'count_text'
    ];

    public function getCountTextAttr($value, $data)
    {
        $cid = $data['id'];
        $count = model('Coupon.Receive')->where(['cid' => $cid])->count();
        return $count;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $createtime = isset($data['createtime']) ? $data['createtime'] : '';

        if(empty($createtime))
        {
            return '';
        }
        
        return date('Y-m-d H:i', $createtime);
    }

    public function getEndtimeTextAttr($value, $data)
    {
        $endtime = isset($data['endtime']) ? $data['endtime'] : '';

        if(empty($endtime))
        {
            return '';
        }
        
        return date('Y-m-d H:i', $endtime);
    }
}
